//
//  ShareViewController.m
//  housingResources
//
//  Created by YYQ on 2017/7/3.
//  Copyright © 2017年 节操君. All rights reserved.
//

#import "ShareViewController.h"
#import <UShareUI/UShareUI.h>

@interface ShareViewController ()
{
    UIView *baseView;
    UIView *bottomView;
}

@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"分享";
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatRightBtn];
    [self creatView];
}

- (void)creatView{
    UIImageView *qrCodeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(100 / kScale, 100 , 550 / kScale, 550 / kScale)];
    qrCodeImgView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:qrCodeImgView];
    
    UILabel *qrCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(100 / kScale, 600 / kScale + 100, 550 / kScale, 100 / kScale)];
    qrCodeLabel.text = @"扫描图中二维码，巴拉巴拉小魔仙。。。。";
    qrCodeLabel.numberOfLines = 0;
    qrCodeLabel.font = [UIFont systemFontOfSize:30 / kScale];
    qrCodeLabel.textColor = [UIColor grayColor];
    qrCodeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:qrCodeLabel];
}

- (void)creatRightBtn{
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setImage:[UIImage imageNamed:@"fenx.png"] forState:UIControlStateNormal];
    shareBtn.frame = CGRectMake(0, 0, 21, 22);
    [shareBtn addTarget:self action:@selector(creatShareView) forControlEvents:UIControlEventTouchUpInside];
    shareBtn.backgroundColor = [UIColor orangeColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:shareBtn];
    self.navigationItem.rightBarButtonItem = item;

}

- (void)creatShareView{
    NSArray *iconArr = @[@"umsocial_wechat",@"umsocial_wechat_timeline",@"umsocial_qq",@"umsocial_qzone"];
    NSArray *titleArr = @[@"微信",@"朋友圈",@"QQ",@"空间"];
    NSArray *tags = @[@"1",@"2",@"4",@"5"];
    
    UIWindow *window = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
    
    baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    baseView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    baseView.userInteractionEnabled = YES;
    [baseView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView)]];
    
    [window addSubview:baseView];
    
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 360 / kScale)];
    bottomView.userInteractionEnabled = YES;
    bottomView.backgroundColor = [UIColor colorWithRed:228 / 255.0 green:235 / 255.0 blue:239 / 255.0 alpha:1];
    [baseView addSubview:bottomView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 70 / kScale)];
    titleLabel.text = @"请选择分享平台";
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [bottomView addSubview:titleLabel];
    
    CGFloat btnWidth = 120 / kScale;
    CGFloat jgWidth = (kScreenWidth - 4 * btnWidth) / 5.0;
    
    for (int i = 0; i < 4; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(jgWidth + (btnWidth + jgWidth) * i, 90 / kScale, btnWidth, btnWidth);
        [btn setImage:[UIImage imageNamed:iconArr[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + [tags[i] integerValue];
        [bottomView addSubview:btn];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(btn.left, btn.bottom, btnWidth, 40 / kScale)];
        titleLabel.text = titleArr[i];
        titleLabel.font = [UIFont systemFontOfSize:24 / kScale];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor grayColor];
        [bottomView addSubview:titleLabel];
    }
    //228  235  239    244  249  251
    UIButton *cannelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cannelBtn.frame = CGRectMake(0, 280 / kScale, kScreenWidth, 80 / kScale);
    [cannelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cannelBtn addTarget:self action:@selector(closeShareView) forControlEvents:UIControlEventTouchUpInside];
    [cannelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    cannelBtn.backgroundColor = [UIColor colorWithRed:224 / 255.0 green:249 / 255.0 blue:251 / 255.0 alpha:1];
    [bottomView addSubview:cannelBtn];
    
    [UIView animateWithDuration:0.3 animations:^{
        bottomView.top = kScreenHeight - 360 / kScale;
    }];
}

- (void)closeShareView{
    [UIView animateWithDuration:0.3 animations:^{
        bottomView.top = kScreenHeight;
    } completion:^(BOOL finished) {
        [baseView removeFromSuperview];
    }];
}

- (void)shareAction:(UIButton *)btn{
    [self shareWebPageToPlatformType:btn.tag - 100];
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欢迎使用【友盟+】社会化组件U-Share" descr:@"欢迎使用【友盟+】社会化组件U-Share，SDK包最小，集成成本最低，助力您的产品开发、运营与推广！" thumImage:thumbURL];
    //设置网页地址
    shareObject.webpageUrl = @"http://mobile.umeng.com/social";
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        //        [self alertWithError:error];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
